1. /* sdec.h by K.Tsuru */
  2. // file id = 50 BRADIX
  3. /****************************************************************************
  4. SDecimal class
  5. Provides a multi-precision fixed-point decimal fraction arithmetric in binary
  6. radix, mainly operations between SDecimal object and small number which is less
  7. than or equal ULONG_MAX/BRADIX.
  8. Used in BPi(), BE(), etc.
  9. [Notice for use]
  10. To obtain a fast speed
  11. 1.size is fixed in maximum length by constructor and can not change the size in scope
  12. 2.in subtraction, do not check which operand is greater than another
  13. 3.does not have exponent, then this class can treat a decimal less than BRADIX
  14. i.e. integer part has one figure : figure[0] < BRADIX
  15. *****************************************************************************/
  16. #ifndef S_DECIMAL_H
  17. #define S_DECIMAL_H
  18. #define AUTO_DConvDec 1 // automaticaly switch in ConvToDec()
  19. #if AUTO_DConvDec
  20. const uint dNconvDecMaxFig = 8000u; // DConvToDec() is faster above this figure
  21. // but out of memory will occure in poor memory system
  22. #endif
  23. class SDecimal : public SDouble, public virtual SCalcInfo {
  24. /*******************************************************************************
  25. Unusable functions
  26. Please use "XXAdd()" etc.
  27. If use, "not accessible" error will occure.
  28. SDouble only, cannot use in this class and has no body.
  29. To make operator+() compiling error, a member function is declared in private part
  30. as follows.
  31. SDecimal operator+(const SDecimal& m) const;
  32. See the reference "Effective C++".
  33. *********************************************************************************/
  34. SDecimal& operator=(const SLong& sl);
  35. SDecimal& operator=(const SFraction& sf);
  36. SDecimal operator+(const SDecimal& m) const;
  37. SDecimal& operator+=(const SDecimal& n);
  38. SDecimal operator-(const SDecimal& m) const;
  39. SDecimal& operator-=(const SDecimal& n);
  40. SDecimal operator/(const SDecimal& m) const;
  41. SDecimal& operator/=(const SDecimal& n);
  42. SDecimal operator/(double d) const;
  43. SDecimal& operator/=(double d);
  44. SDecimal& operator=(const char *s);
  45. SDecimal(const char *s);
  46. void SetRdxExp(int e);
  47. long DExp() const;
  48. long DFigures() const;
  49. void StdReform(int id);
  50. SDecimal DReciprocal(const SDecimal& x);
  51. void FixedPoint(int exp);
  52. void PointFree();
  53. //can not change the size
  54. void SizeZero();
  55. protected:
  56. void SetXDouble(double d); // Set a double, long, etc. value// 501
  57. public:
  58. // radix conversion
  59. SDecimal ConvToBin(const SDouble& m); // DRADIX --> BRADIX 502 changed to member since version 2.20
  60. // normal radix conversion
  61. SDouble NConvToDec() const; // BRADIX --> DRADIX 503
  62. /************************
  63. Divided radix conversion
  64. *************************/
  65. SDouble DConvToDec() const; // BRADIX --> DRADIX 504
  66. /************************************
  67. Radix conversion SDecimal to SDouble
  68. using "square coupling" method see below
  69. *************************************/
  70. // SDouble has also SetInt().
  71. void SetInt(int a){
  72. if(abs(a) >= Radix()) SetError(OUT_OF_RANGE, "SX SetInt", 51);
  73. SetZero(); figure[0] = (fType)abs(a); SetSign(a);
  74. }
  75. SDouble ConvToDec() const {
  76. #if AUTO_DConvDec
  77. if(aHead + 1 > dNconvDecMaxFig) return DConvToDec();
  78. #endif
  79. return NConvToDec();
  80. }
  81. // constructor
  82. SDecimal():SDouble(BIN_DEC, SNMaxSize(BIN_DEC)){}
  83. SDecimal(double d):SDouble(BIN_DEC, SNMaxSize(BIN_DEC)){
  84. SetXDouble(d);
  85. }
  86. // copy constructor
  87. SDecimal(const SDecimal& a):SDouble(a){}
  88. // SDouble copy constructor
  89. // Note that this is not a radix conversion function.
  90. // "a" must have type BIN_DEC.
  91. SDecimal(const SDouble& a):SDouble(a){
  92. if(a.Type() != BIN_DEC) SetError(RADIX_ERR, "SX(SD)", 51);
  93. }
  94. // destructor
  95. ~SDecimal(){}
  96. SDecimal& operator=(double d){ SetXDouble(d); return *this; }
  97. // for use of SDouble class's multiplication
  98. // "a" must have type BIN_DEC.
  99. SDecimal& operator=(const SDouble& a){
  100. if(a.Type() != BIN_DEC) SetError(RADIX_ERR, "SX = SD", 52);
  101. SNumber::CopyValue(a, SUBS);
  102. return *this;
  103. }
  104. SDecimal& operator=(const SDecimal& a){
  105. if(this != &a) SNumber::CopyValue(a, SUBS);
  106. return *this;
  107. }
  108. /******************************************************************
  109. sign operator
  110. If this operator is not provided, substitution m = -n is interpreted
  111. as SDouble's operator,i.e. SDecimal = SDouble, and a compiling error occure.
  112. ******************************************************************/
  113. SDecimal operator+() const { return *this; }
  114. SDecimal operator-() const; // 505
  115. SDecimal& BitShift(long p); // bit shift operator *(2^p). p < 0 Ok 506
  116. // SDecimal class object never has sign = UNDECIDED, because initialized by zero.
  117. // To be consistent with SDouble::Sign(int)
  118. int Sign(int=50) const { return RawSign(); }
  119. // friend functions
  120. // to avoid the cost of memory copy, obtain result in a argument "SDecimal& r"
  121. // then operator+() etc. are not provided
  122. friend void XXAdd(const SDecimal& m, const SDecimal& n, SDecimal& r);//r=m+n 507
  123. friend void XXSub(const SDecimal& m, const SDecimal& n, SDecimal& r);//r=m-n(|m|>=|n|) 508
  124. //operation with small s, s <= ULONG_MAX/BRADIX
  125. friend void XsMult(const SDecimal& m, ulong s, SDecimal& r); // r=m*s 509
  126. friend void XsDiv(const SDecimal& m, ulong s, SDecimal& r); // r=m/s 510
  127. //converting to SDouble and output to present stream
  128. long Put(long fig=5, long pr = 0, int perLine=0, int mode = CRLF|ROUND|INT_PUT, int delmt=' ') const;
  129. long Puts(long fig=5,long pr = 0, int perLine=0, int mode = CRLF|ROUND|INT_PUT, int delmt=' ') const;
  130. };
  131. // b = a*2^p
  132. inline void BitShift(const SDecimal&a , long p, SDecimal& b){
  133. b = a; b.BitShift(p);
  134. }
  135. inline long SDecimal::Puts(long fig, long pr, int perLine, int mode, int delmt) const{
  136. return SDecimal::Put(fig, pr, perLine, mode|END_CR, delmt);
  137. }
  138. bool DSeriesIsFast(const SDouble& x);
  139. // Representation for 1/DRADIX in BRADIX
  140. SDecimal BRecDRadix(); // 511
  141. void DrdxFree();
  142. /************************************
  143. Radix conversion SDecimal to SDouble
  144. using "square coupling" method
  145. not member function
  146. *************************************/
  147. SDouble SC_ConvSDecToSDbl(const SDecimal& x); // 512
  148. /***************
  149. using binary splitting template class
  150. ****************/
  151. SDouble BS_ConvSDecToSDbl(const SDecimal& x);
  152. #endif // S_DECIMAL_H

sdec.h : last modifiled at 2017/11/06 15:04:18(6,341 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/11/06 15:07:45 (Mon Nov 06 15:07:45 2017).